package com.vip.iot.util;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.util.Properties;

/**
 * description
 *
 * @author wgb
 * @date 2020/6/18 11:18
 */
public class PropertiesUtils {
    private static Properties dataProp;

    /**
     * 获取指定配置文件中的指定属性
     *
     * @param propertyName
     * @param key
     * @return
     */
    public static Object getYmlFileProperty(String propertyName, Object key){
        Resource resource = new ClassPathResource(propertyName);
        Properties properties;
        try {
            YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            yamlFactory.setResources(resource);
            properties =  yamlFactory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return properties.get(key);
    }

    public static void main(String[] args) {
        System.out.println(getYmlFileProperty("application.yml", "mqtt.server.username"));
    }
}
